﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ueEngine.Framework.Common
{
    public class EMail
    {
        private EmailContext emailItem;
        public EMail(EmailContext emailItem)
        {
            this.emailItem = emailItem;
        }
        public void Send()
        {
            System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
            Encoding eEncod = Encoding.GetEncoding("utf-8");
            if (String.IsNullOrEmpty(this.emailItem.DisplayName))
            {
                this.emailItem.DisplayName = "重庆大学生兼职网 -- www.yaojianzhi.com";
            }
            myEmail.From = new System.Net.Mail.MailAddress(this.emailItem.From.Trim(), this.emailItem.DisplayName, eEncod);
            myEmail.To.Add(this.emailItem.Recipient.Trim());
            myEmail.Subject = this.emailItem.Subject.Trim();
            myEmail.IsBodyHtml = true;
            myEmail.Body = this.emailItem.Body.Trim();
            myEmail.Priority = System.Net.Mail.MailPriority.Normal;
            myEmail.BodyEncoding = Encoding.GetEncoding("utf-8");


            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = "smtp." + myEmail.From.Host;
            if (smtp.Host.ToLower() == "smtp.yaojianzhi.com")
            {
                smtp.Host = "smtp.exmail.qq.com";
                smtp.Port = 25;
            }
            //if (smtp.Host.ToLower() == "smtp.gmail.com")
            //{
            //    smtp.EnableSsl = true;//SMTP服务器是否启用SSL加密
            //}
            smtp.Credentials = new System.Net.NetworkCredential(this.emailItem.CredentialUserName, this.emailItem.CredentialPassword);
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            try
            {
                smtp.Send(myEmail);
                LogHelper.Write(CommonLogger.System, LogLevel.Trace,
                    "MailForm:" + this.emailItem.From.Trim() +
                    "MailTo:" + this.emailItem.Recipient.Trim() + Environment.NewLine);
            }
            catch (System.Net.Mail.SmtpException e)
            {
                LogHelper.Write(CommonLogger.UserSystem, LogLevel.Error,
                    "MailForm:" + this.emailItem.From.Trim() + Environment.NewLine +
                    "MailTo:" + this.emailItem.Recipient.Trim() + Environment.NewLine +
                    e.ToString());
                //throw new ContextException(String.Format("发送邮件至:{0}失败", this.emailItem.Recipient), ExceptionCode.OperationFaild);
            }
        }

    }

    public class EmailContext
    {
        /// <summary>
        /// 邮件主题
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// 发件箱
        /// </summary>
        public string From { get; set; }
        /// <summary>
        /// 收件箱地址
        /// </summary>
        public string Recipient { get; set; }
        /// <summary>
        /// 关联显示名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body { get; set; }
        /// <summary>
        /// 发件箱账号
        /// </summary>
        public string CredentialUserName { get; set; }
        /// <summary>
        /// 发件箱密码
        /// </summary>
        public string CredentialPassword { get; set; }
    }
}
